package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.model.vo.req.UserVo;
import com.moli.iov.admin.service.IAuthenticationFacadeService;
import com.moli.iov.admin.service.IUserFacadeService;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.sys.enums.UserTypeEnum;
import com.moli.iov.sys.model.po.User;
import com.moli.iov.util.StringUtil;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户管理
 * </p>
 *
 * @author zql
 * @since 2019/9/9 14:53
 */
@Slf4j
@RestController
@RequestMapping("/auth")
public class UserController extends BaseController {

  @Autowired
  private IUserFacadeService userFacadeService;

  @Autowired
  private IAuthenticationFacadeService authenticationFacadeService;

  @Value("${jwt.tokenHeader}")
  private String jwtTokenHeader;

  @Value("${jwt.tokenHead}")
  private String tokenHead;

  /**
   * 分页查询用户列表
   * @param userVo
   * @return
   */
  @RequestMapping("/user/platform/list")
  public RestResponse<PageExtend<User>> listUserOfPage(@RequestBody(required = false) UserVo userVo){
    if(userVo == null){
      userVo = new UserVo();
    }
    assembleUserInfo(userVo);
    if(UserTypeEnum.COMPANY.getFlag() == userVo.getSysUserType()){
      return RestResponse.failure("无权限");
    }
    userVo.setUserType(UserTypeEnum.PLATFORM.getFlag());
    return userFacadeService.listUserOfPage(userVo);
  }

  /**
   * 添加用户
   * @param userVo
   * @return
   */
  @RequestMapping("/user/platform/add")
  public RestResponse saveUser(@RequestBody @Validated({UserVo.Save.class}) UserVo userVo){
    assembleUserInfo(userVo);
    userVo.setUserType(UserTypeEnum.PLATFORM.getFlag());
    if(UserTypeEnum.COMPANY.getFlag() == userVo.getSysUserType()){
      return RestResponse.failure("无权限");
    }
    log.info("添加用户入参：{}", userVo);
    return userFacadeService.saveUser(userVo);
  }

  /**
   * 修改用户
   * @param userVo
   * @return
   */
  @RequestMapping("/user/platform/update")
  public RestResponse updateUser(@RequestBody @Validated({UserVo.Update.class}) UserVo userVo){
    assembleUserInfo(userVo);
    if(UserTypeEnum.COMPANY.getFlag() == userVo.getSysUserType()){
      return RestResponse.failure("无权限");
    }
    return userFacadeService.updateUser(userVo);
  }

  /**
   * 重置密码
   * @param userVo
   * @return
   */
  @RequestMapping("/user/platform/reset")
  public RestResponse resetPwd(@RequestBody @Validated({UserVo.Reset.class}) UserVo userVo){
    assembleUserInfo(userVo);
    return userFacadeService.resetPwd(userVo);
  }

  /**
   * 修改密码
   * @param userVo
   * @return
   */
  @RequestMapping("/user/updatePwd")
  public RestResponse updatePwdO(@RequestBody @Validated({UserVo.UpdatePwd.class}) UserVo userVo){
    assembleUserInfo(userVo);
    return userFacadeService.updatePwd(userVo);
  }

  /**
   * 分页查询用户列表
   * @param userVo
   * @return
   */
  @RequestMapping("/user/company/list")
  public RestResponse<PageExtend<User>> listUserOfPageOfCompany(@RequestBody(required = false) UserVo userVo){
    if(userVo == null){
      userVo = new UserVo();
    }
    assembleUserInfo(userVo);

    userVo.setUserType(UserTypeEnum.COMPANY.getFlag());
    return userFacadeService.listUserOfPage(userVo);
  }

  /**
   * 添加用户
   * @param userVo
   * @return
   */
  @RequestMapping("/user/company/add")
  public RestResponse saveUserOfCompany(@RequestBody @Validated({UserVo.Save.class}) UserVo userVo){
    assembleUserInfo(userVo);
    userVo.setUserType(UserTypeEnum.COMPANY.getFlag());

    if(UserTypeEnum.PLATFORM.getFlag() == userVo.getSysUserType() && userVo.getCompanyId() == null){
      return RestResponse.failure("企业id不能为空");
    }

    if(UserTypeEnum.COMPANY.getFlag() == userVo.getSysUserType()){
      userVo.setCompanyId(userVo.getSysCompanyId());
      userVo.setCompanyName(userVo.getSysCompanyName());
    }
    log.info("添加用户入参：{}", userVo);
    return userFacadeService.saveUser(userVo);
  }

  /**
   * 修改用户
   * @param userVo
   * @return
   */
  @RequestMapping("/user/company/update")
  public RestResponse updateUserOfCompany(@RequestBody @Validated({UserVo.Update.class}) UserVo userVo){
    assembleUserInfo(userVo);
    return userFacadeService.updateUser(userVo);
  }

  /**
   * 重置密码
   * @param userVo
   * @return
   */
  @RequestMapping("/user/company/reset")
  public RestResponse resetPwdOfCompany(@RequestBody @Validated({UserVo.Reset.class}) UserVo userVo){
    assembleUserInfo(userVo);
    return userFacadeService.resetPwd(userVo);
  }

  /**
   * 登录
   * @param userVo
   * @return
   */
  @RequestMapping("/user/login")
  public RestResponse loginOfCompany(@RequestBody @Validated({UserVo.Login.class}) UserVo userVo, HttpServletRequest req){
    return authenticationFacadeService.login(req, userVo);
  }

  /**
   * 登出
   * @return
   */
  @RequestMapping("/user/logout")
  public RestResponse logout(HttpServletRequest req){
    String authHeader = req.getHeader(jwtTokenHeader);
    if(StringUtil.isBlank(authHeader)){
      log.info("登出 token为空 authHeader:{}", authHeader);
      return RestResponse.success();
    }

    try{
      String token = authHeader.substring(this.tokenHead.length());
      authenticationFacadeService.logout(token);
    }catch(Exception e){
      log.error("登出异常", e);
    }
    return RestResponse.success();
  }
}
